package edu.just.pet.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.util.StringUtil;
import edu.just.pet.entity.SysUser;
import edu.just.pet.entity.User;
import edu.just.pet.service.SysUserService;
import edu.just.pet.system.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("sysuser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;
    @RequestMapping("login.do")
    @ResponseBody
    public Map login(SysUser sysUser, HttpSession session) {

        System.out.println("logining:"+sysUser);
        Map<String, Object> result = new HashMap<>();

        // 用户名密码不能为空
        if(StrUtil.isEmpty(sysUser.getUsername()) || StringUtil.isEmpty(sysUser.getPassword())) {
            result.put(Constants.RETURN_CODE, Constants.CODE_FAIL);
            result.put(Constants.RETURN_MSG, Constants.MSG_ERROR);
            return result;
        }

        // 调用service，去查找用户是否存在
        SysUser sysUserInDb = sysUserService.check(sysUser);
        // 比较加密后的密码是否相等
//        String encrypPassword = SecureUtil.md5(sysUser.getPassword());
        System.out.println("sysUserInDb"+sysUserInDb);
        if(sysUserInDb != null && sysUserInDb.getPassword().equals(sysUser.getPassword())) {
            session.setAttribute("sysLoginName",sysUserInDb.getUsername());
            session.setAttribute("sysLoginId",sysUserInDb.getId());
            session.setAttribute("status","admin");

            result.put(Constants.RETURN_DATA,sysUserInDb);
            result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
            result.put(Constants.RETURN_MSG, Constants.MSG_SUCCESS);
        }else {
            result.put(Constants.RETURN_CODE, Constants.CODE_ERROR);
            result.put(Constants.RETURN_MSG, Constants.MSG_ERROR);
        }

        return result;
    }
    @RequestMapping("logout.do")
    @ResponseBody
    public Map logout(HttpSession session){
        Map<String,Object> result=new HashMap<>();
        session.invalidate();
        result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
        result.put(Constants.RETURN_MSG, Constants.MSG_SUCCESS);
        return result;
    }

    @RequestMapping("afterLogin.do")
    @ResponseBody
    public ModelAndView afterLogin(SysUser sysUser){
        Map<String, Object> result = new HashMap<>();
        System.out.println(sysUser);
        SysUser sysUserInDb=sysUserService.getSysUserInfoById(sysUser);
        result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
        result.put(Constants.RETURN_MSG, Constants.MSG_SUCCESS);
        result.put(Constants.RETURN_DATA,sysUserInDb);

        return new ModelAndView("/back/adminhomepage",result);
    }
}
